<?php
/**************************
 *
 * 名  称   数据库链接类
 * 描  述   mysql数据库链接类
 * 关联表
 *
 * 编写人   
 * 日  期   
 *
 * 修改人  
 * 日  期  
 * 修改描述 
***************************/

class Mysql_DB{
	var $db_link = NULL;
	var $db_res = NULL;
    var $db_name = NULL;
    var $database = NULL;
	var $charset = NULL;
	
	function Mysql_DB($new_link = false){
        global $mysql_config;
		$this->db_name = 'ADS';
        $this->database = $mysql_config[$this->db_name]['database'];
        $this->connect($new_link);
    }
	

	function connect($new_link = false){
		global $mysql_config;
        if($this->db_link == NULL){
            $this->db_link = @mysql_connect($mysql_config[$this->db_name]['host'],$mysql_config[$this->db_name]['user'],$mysql_config[$this->db_name]['pass'],$new_link);
		    if($this->db_link == false){
			    $this->halt('Can not Connect to Mysql Server!');
		    }
		    $this->select_db($this->database,$this->db_link);
        }
	}

	function mysql_charset($charset = NULL,$db_link = NULL){
		if($charset){
			$this->charset = $charset;
			if($db_link == NULL){
				if($this->db_link == NULL){
					$this->halt('Function Query() Missing Link Identifier!');
				}
				$db_link = $this->db_link;
			}
			if($this->version() > '4.1'){
				if($this->charset){
					@mysql_query("SET character_set_connection=$this->charset, character_set_results=$this->charset, character_set_client=binary", $db_link);
				}
				if($this->version() > '5.0.1'){
					@mysql_query("SET sql_mode=''", $db_link);
				}
			}
		}
	}

	function select_db($db_name = NULL , $db_link = NULL){
		if($db_name == NULL){
		 	$this->halt('Function select_db() Missing db_name!');
		}
		if($db_link == NULL){
			if($this->db_link == NULL){
		  		$this->halt('Function select_db() Missing Link Identifier!');
			}
			$db_link = $this->db_link;
		}
		if(mysql_select_db($db_name,$db_link) == false){
		 	$this->halt("Can not Select Database '$db_name'!");
		}
	}

	function query($sql_query = NULL , $db_link = NULL){
		if($sql_query == NULL){
			$this->halt('Function Query() Missing SQL Query!');
		}
		if($db_link == NULL){
			if($this->db_link == NULL){
		  		$this->halt('Function Query() Missing Link Identifier!');
			}
			$db_link = $this->db_link;
		}
		//if(mysql_select_db($this->database,$db_link) == false){
		 	//$this->halt("Can not Select Database '$this->database'!");
		//}
		$this->db_res = @mysql_query($sql_query , $db_link); 
		if($this->db_res == false){
			$this->halt('Invalid SQL Words or Query Error!' , $sql_query);
		}
		return $this->db_res;
	}

	function next_record($db_res = NULL){
		if($db_res == NULL){
			if($this->db_res == NULL){
		  		$this->halt('Function  next_record() Missing Resouce link_identifier!');
			}
			$db_res = $this->db_res;
		}
		return mysql_fetch_array($db_res);
	}

	function num_rows($db_res = NULL){
		if($db_res == NULL){
			if($this->db_res == NULL){
		  		$this->halt('Function  num_rows() Missing Resouce link_identifier!');
			}
			$db_res = $this->db_res;
		}
		return mysql_num_rows($db_res);
	}

	function affected_rows(){		
	    if($this->db_link == NULL){
		  	$this->halt('Function  affected_rows() Missing Resouce link_identifier!');
		}
		$db_link = $this->db_link;
		return mysql_affected_rows($db_link);
	}

	function halt($error_msg,$sql=NULL){
       
			echo $error_msg;
           exit("数据库错误！");
	 
	}

    function close(){
		if($this->db_link == NULL){
		  	$this->halt('Function close() Missing Link Identifier!');
		}
        $result = @mysql_close($this->db_link);
        if($result == FALSE){
            $this->halt('Can not close Current db_link!');
        }else{
            $this->db_link = NULL;
        }
    }

    function insert_id(){
    	if($this->db_link == NULL){
		  	$this->halt('Function close() Missing Link Identifier!');
		}
        return mysql_insert_id($this->db_link);
    }

    function fetch_array($db_res = NULL){
		if($db_res == NULL){
			if($this->db_res == NULL){
		  		$this->halt('Function  next_record() Missing Resouce link_identifier!');
			}
			$db_res = $this->db_res;
		}
		return mysql_fetch_array($db_res);
	}
   
    function result($query, $row){
		$query = @mysql_result($query, $row);
		return $query;
	}
	
	function version() {
		return mysql_get_server_info($this->db_link);
	}
}


?>
